package ru.ifmo.sound.jmlbased;

import net.sf.javaml.core.Dataset;
import net.sf.javaml.core.DenseInstance;
import net.sf.javaml.core.Instance;
import net.sf.javaml.distance.ChebychevDistance;
import net.sf.javaml.distance.PearsonCorrelationCoefficient;
import net.sf.javaml.featureselection.subset.GreedyForwardSelection;
import net.sf.javaml.filter.DatasetFilter;
import net.sf.javaml.filter.InstanceFilter;
import net.sf.javaml.filter.RetainAttributes;

import java.util.Date;


/**
 * @author avhaliullin
 */
public class FeatureSelector {
    public static final int inputs = 300;
    private static final int samplesPerTrack = 12;
    private InstanceFilter instanceFilter;
    private DatasetFilter datasetFilter;
    private DatasetGenerator datasetGenerator;

    public FeatureSelector(DatasetGenerator datasetGenerator) {
        this.datasetGenerator = datasetGenerator;
        log("Generating dataset...");
        Dataset dataset = datasetGenerator.generateDataset(samplesPerTrack);
        log("Feature selection...");
        GreedyForwardSelection selection = new GreedyForwardSelection(inputs, new PearsonCorrelationCoefficient());
        selection.build(dataset);
        datasetFilter = new RetainAttributes(selection.selectedAttributes());
        instanceFilter = new RetainAttributes(selection.selectedAttributes());
    }

    public Dataset createDataset(int samplesPerTrack) {
        Dataset dataset = datasetGenerator.generateDataset(samplesPerTrack);
        datasetFilter.filter(dataset);
        return dataset;
    }

    public double[] filter(double[] vector) {
        Instance instance = new DenseInstance(vector);
        instanceFilter.filter(instance);
        return WrapperMethods.instanceToArray(instance);
    }

    private static void log(String s) {
        System.out.println(new Date() + " " + s);
    }
}
